home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / CharField.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  6.6 KB  |  257 lines

  1. package symantec.itools.db.net;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import symjava.lang.Bignum;
  8. import symjava.sql.Date;
  9. import symjava.sql.SQLException;
  10. import symjava.sql.Time;
  11. import symjava.sql.Timestamp;
  12.  
  13. abstract class CharField extends Field {
  14.    String _data;
  15.  
  16.    void read(DataInputStream in) throws SQLException, IOException, ErrorException {
  17.       super.read(in);
  18.       this._data = new String("");
  19.    }
  20.  
  21.    void write(DataOutputStream out) throws IOException {
  22.       super.write(out);
  23.    }
  24.  
  25.    public String getString() throws SQLException {
  26.       return ((Field)this).isNull() ? null : new String(this._data);
  27.    }
  28.  
  29.    public boolean getBoolean() throws SQLException {
  30.       if (((Field)this).isNull()) {
  31.          return false;
  32.       } else {
  33.          byte b = (byte)this._data.charAt(0);
  34.          return b != 0;
  35.       }
  36.    }
  37.  
  38.    public byte getByte() throws SQLException {
  39.       if (((Field)this).isNull()) {
  40.          return 0;
  41.       } else {
  42.          try {
  43.             Double d = new Double(this._data);
  44.             return (byte)d.intValue();
  45.          } catch (Exception e) {
  46.             throw new SQLException(((Throwable)e).getMessage());
  47.          }
  48.       }
  49.    }
  50.  
  51.    public short getShort() throws SQLException {
  52.       if (((Field)this).isNull()) {
  53.          return 0;
  54.       } else {
  55.          try {
  56.             Double d = new Double(this._data);
  57.             return (short)d.intValue();
  58.          } catch (Exception e) {
  59.             throw new SQLException(((Throwable)e).getMessage());
  60.          }
  61.       }
  62.    }
  63.  
  64.    public int getInt() throws SQLException {
  65.       if (((Field)this).isNull()) {
  66.          return 0;
  67.       } else {
  68.          try {
  69.             Double d = new Double(this._data);
  70.             return d.intValue();
  71.          } catch (Exception e) {
  72.             throw new SQLException(((Throwable)e).getMessage());
  73.          }
  74.       }
  75.    }
  76.  
  77.    public long getLong() throws SQLException {
  78.       if (((Field)this).isNull()) {
  79.          return 0L;
  80.       } else {
  81.          try {
  82.             Double d = new Double(this._data);
  83.             return d.longValue();
  84.          } catch (Exception e) {
  85.             throw new SQLException(((Throwable)e).getMessage());
  86.          }
  87.       }
  88.    }
  89.  
  90.    public float getFloat() throws SQLException {
  91.       if (((Field)this).isNull()) {
  92.          return 0.0F;
  93.       } else {
  94.          try {
  95.             Double d = new Double(this._data);
  96.             return d.floatValue();
  97.          } catch (Exception e) {
  98.             throw new SQLException(((Throwable)e).getMessage());
  99.          }
  100.       }
  101.    }
  102.  
  103.    public double getDouble() throws SQLException {
  104.       if (((Field)this).isNull()) {
  105.          return (double)0.0F;
  106.       } else {
  107.          try {
  108.             Double d = new Double(this._data);
  109.             return d;
  110.          } catch (Exception e) {
  111.             throw new SQLException(((Throwable)e).getMessage());
  112.          }
  113.       }
  114.    }
  115.  
  116.    public Bignum getBignum(int scale) throws SQLException {
  117.       return ((Field)this).isNull() ? null : new Bignum(this._data, scale);
  118.    }
  119.  
  120.    public byte[] getBytes() throws SQLException {
  121.       if (((Field)this).isNull()) {
  122.          return new byte[0];
  123.       } else {
  124.          try {
  125.             byte[] dst = new byte[this._data.length()];
  126.             this._data.getBytes(0, this._data.length(), dst, 0);
  127.             return dst;
  128.          } catch (Exception e) {
  129.             throw new SQLException(((Throwable)e).getMessage());
  130.          }
  131.       }
  132.    }
  133.  
  134.    public Date getDate() throws SQLException {
  135.       return ((Field)this).isNull() ? null : Date.valueOf(this._data);
  136.    }
  137.  
  138.    public Time getTime() throws SQLException {
  139.       return ((Field)this).isNull() ? null : Time.valueOf(this._data);
  140.    }
  141.  
  142.    public Timestamp getTimestamp() throws SQLException {
  143.       return ((Field)this).isNull() ? null : Timestamp.valueOf(this._data);
  144.    }
  145.  
  146.    public InputStream getAsciiStream() throws SQLException {
  147.       return ((Field)this).isNull() ? null : new BinaryInputStream(this._data);
  148.    }
  149.  
  150.    public InputStream getUnicodeStream() throws SQLException {
  151.       return ((Field)this).isNull() ? null : new BinaryInputStream(this._data);
  152.    }
  153.  
  154.    public InputStream getBinaryStream() throws SQLException {
  155.       return ((Field)this).isNull() ? null : new BinaryInputStream(this._data);
  156.    }
  157.  
  158.    public void setBoolean(boolean x) throws SQLException {
  159.       if (x) {
  160.          this._data = new String("true");
  161.       } else {
  162.          this._data = new String("false");
  163.       }
  164.  
  165.       super._null = false;
  166.    }
  167.  
  168.    public void setByte(byte x) throws SQLException {
  169.       this._data = Integer.toString(x);
  170.       super._null = false;
  171.    }
  172.  
  173.    public void setShort(short x) throws SQLException {
  174.       this._data = Integer.toString(x);
  175.       super._null = false;
  176.    }
  177.  
  178.    public void setInt(int x) throws SQLException {
  179.       this._data = Integer.toString(x);
  180.       super._null = false;
  181.    }
  182.  
  183.    public void setLong(long x) throws SQLException {
  184.       this._data = Long.toString(x);
  185.       super._null = false;
  186.    }
  187.  
  188.    public void setFloat(float x) throws SQLException {
  189.       this._data = Float.toString(x);
  190.       super._null = false;
  191.    }
  192.  
  193.    public void setDouble(double x) throws SQLException {
  194.       this._data = Double.toString(x);
  195.       super._null = false;
  196.    }
  197.  
  198.    public void setBignum(Bignum x) throws SQLException {
  199.       this._data = x.toString();
  200.       super._null = false;
  201.    }
  202.  
  203.    public void setString(String x) throws SQLException {
  204.       this._data = x;
  205.       super._null = false;
  206.    }
  207.  
  208.    public void setBytes(byte[] x) throws SQLException {
  209.       this._data = new String(x, 0);
  210.       super._null = false;
  211.    }
  212.  
  213.    public void setDate(Date x) throws SQLException {
  214.       this._data = x.toString();
  215.       super._null = false;
  216.    }
  217.  
  218.    public void setTime(Time x) throws SQLException {
  219.       this._data = x.toString();
  220.       super._null = false;
  221.    }
  222.  
  223.    public void setTimestamp(Timestamp x) throws SQLException {
  224.       this._data = x.toString();
  225.       super._null = false;
  226.    }
  227.  
  228.    public void setStream(InputStream x, int length) throws SQLException {
  229.       if (length >= 0 && length <= 255) {
  230.          byte[] b = new byte[length];
  231.  
  232.          try {
  233.             x.read(b, 0, length);
  234.          } catch (IOException e) {
  235.             throw new SQLException(((Throwable)e).getMessage());
  236.          }
  237.  
  238.          this._data = new String(b, 0, 0, b.length);
  239.          super._null = false;
  240.       } else {
  241.          throw new SQLException("Stream length out of range.");
  242.       }
  243.    }
  244.  
  245.    public void setAsciiStream(InputStream x, int length) throws SQLException {
  246.       this.setStream(x, length);
  247.    }
  248.  
  249.    public void setUnicodeStream(InputStream x, int length) throws SQLException {
  250.       this.setStream(x, length);
  251.    }
  252.  
  253.    public void setBinaryStream(InputStream x, int length) throws SQLException {
  254.       this.setStream(x, length);
  255.    }
  256. }
  257.